<template>
  <section class="msite">
<!--首页头部-->
    <HeaderTop :title="address.name">
      <router-link class="header_search" slot="left" to="/search">
        <i class="iconfont icon-sousuo"></i>
      </router-link>
      <router-link class="header_login" slot="right" :to="userInfo._id ? '/userinfo': '/login'">
        <span class="header_login_text" v-if="!userInfo._id">
          登录|注册
        </span>
        <span class="header_login_text" v-else>
           <i class="iconfont icon-person"></i>
        </span>
      </router-link>
    </HeaderTop>
<!-- 轮播图-->
    <div class="miste-content-wrapper">
      <div class="miste-content">
        <!--首页导航-->
        <nav class="msite_nav">
          <div class="swiper-container" v-if="categorys.length">
            <div class="swiper-wrapper">
              <div class="swiper-slide" v-for="(category, index) in categorys" :key="index">
                <a href="javascript:" class="link_to_food" >
                  <div class="food_container">
                    <img :src="baseImageUrl+category.image_url">
                  </div>
                </a>
              </div>
            </div>
            <!-- Add Pagination -->
            <div class="swiper-pagination"></div>
          </div>
          <img src="./images/msite_back.svg" alt="back" v-else>
        </nav>
<!-- 列表 -->
		<div class="dh">
		    <div class="a" @click="gotoHouse">
		    	<div class="image1 image"></div>
		    	<div>房源管理</div>
		    </div>
		    <div class="a" @click="comevent(true)">
		    	<div class="image2 image"></div>
		    	<div>手机开门</div>
		    </div>
		    <div class="a" @click="m(2)">
		    	<div class="image3 image"></div>
		    	<div>账单管理</div>
		    </div>
			<div class="a" @click="m(4)">
				<div class="image4 image"></div>
				<div>房屋保修</div>
			</div>
			<div class="a" @click="comevent(true)">
				<div class="image5 image"></div>
				<div>代办事项</div>
			</div>
			<div class="a" @click="m(2)">
				<div class="image6 image"></div>
				<div>楼栋管理</div>
			</div>
		</div>
 <!--首页附近房源-->
        <div class="msite_shop_list">
          <div class="shop_header">
            <i class="iconfont icon-xuanxiang"></i>
            <span class="shop_header_title">附近房源</span>
          </div>
              <div class="swiper-container-bottom" >
                <div class="swiper-wrapper shop_lis" v-if="nearHouse.length">
                  <div class="swiper-slide shop_li" v-for="(list,i) in nearHouse" :key="i" @click="$router.push('/shop')">
                
                      <a>
                       <div class="houseList"  data-type="goods" :data-id="list.id" :data-index="i">
                        <img :src="list.img_url1" class="hosImg"/>
                        <div class="detail">
                            <div class="hostitle">{{list.title}}</div>
                            <div class="hosproportion">{{list.proportion}}m²/{{list.bedroom}}房{{list.hall}}</div>
                            <div class="hosrentPrice"><span>¥</span>{{list.rent_price}}<span>/月</span></div>
                            <div class="label">
                                <div class="hosbetp" >押{{list.bet}}付{{list.pay}}</div>
                                <div v-for="(item,index) in list.lableList" :key="index">
                                    <div class="hosbetp" style="background-color: #f4f4f4color: #999999">{{item.lableName}}</div>
                                </div>
                            </div>
                        </div>
                       </div>
                      </a>
                   </div>
                </div>
              </div>       
        </div>
        
      </div>
    </div>
  </section>
</template>

<script>
  import BScroll from 'better-scroll'
  import {mapState} from 'vuex'
  import Swiper from 'swiper'
  import 'swiper/dist/css/swiper.min.css'
  import HeaderTop from '../../components/HeaderTop/HeaderTop.vue'

  export default {
    data () {
      return {
        baseImageUrl: 'https://gitee.com/xiuya/bjz-img'
      }
    },
    mounted () {

      this.$store.dispatch('getCategorys')
      this.$store.dispatch('getShops')
      this.$store.dispatch('getNearhouse')
      
      if(navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(
              function (position) {  
                  console.log( position.coords.longitude )
                  console.log( position.coords.latitude )
              },
              function (e) {
                 throw(e.message)
              }
          ) 
      }
      
      
      
      console.log(this.nearHouse)
    },
    methods:{
        gotoHouse(){
            this.$router.push('/housingManagement')
        }
    },
    computed: {
      ...mapState(['address', 'categorys', 'userInfo','shops','nearHouse']),

      /*
      根据categorys一维数组生成一个2维数组
      小数组中的元素个数最大是8
       */
      categorysArr () {
        const {categorys} = this
        // 准备空的2维数组
        const arr = []
        // 准备一个小数组(最大长度为8)
        let minArr = []
        // 遍历categorys
        categorys.forEach(c => {
          // 如果当前小数组已经满了, 创建一个新的
          if(minArr.length===8) {
            minArr = []
          }
          // 如果minArr是空的, 将小数组保存到大数组中
          if(minArr.length===0) {
            arr.push(minArr)
          }
          // 将当前分类保存到小数组中
          minArr.push(c)
        })

        return arr
      },
      houseArr(){
           const {nearHouse} = this
           console.log(nearHouse)
           const arr = []
           return arr
      }
      
    },

    watch: {
      categorys (value) { // categorys数组中有数据了, 在异步更新界面之前执行
        // 界面更新就立即创建Swiper对象
        this.$nextTick(() => {// 一旦完成界面更新, 立即调用(此条语句要写在数据更新之后)
          // 创建一个Swiper实例对象, 来实现轮播
          new Swiper('.swiper-container', {
            loop: true, // 可以循环轮播
            // 如果需要分页器
            pagination: {
              el: '.swiper-pagination',
            },
          })
          new BScroll('.miste-content-wrapper', {
            click: true
          })
          
        })

      },
      nearHouse(){
          this.$nextTick(() => {// 一旦完成界面更新, 立即调用(此条语句要写在数据更新之后)
                new Swiper('.swiper-container-bottom', {
                  loop: true, // 可以循环轮播
                  // 如果需要分页器
                   slidesPerView : 2,  

                })
                new BScroll('.swiper-container-bottom', {
                  click: true
                })
          })
      }
    },

    components: {
      HeaderTop
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus">
  @import "../../common/stylus/mixins.styl"
  .msite  //首页
    width 100%
    .miste-content-wrapper
      position fixed
      top: 50px
      bottom: 46px
      width: 100%
      .msite_nav
        bottom-border-1px(#e4e4e4)
        margin-top 15px
        height 200px
        background #fff
        .swiper-container
          width 94%
          height 200px
          border-radius 10px
          .swiper-wrapper
            width 100%
            height 200px
            .swiper-slide
              display flex
              justify-content center
              align-items flex-start
              flex-wrap wrap
              .link_to_food
                width 100%
                height 100%
                .food_container
                  display block
                  width 100%
                  height 100%
                  text-align center
                  padding-bottom 10px
                  font-size 0
                  img
                    display inline-block
                    width 100%
                    height 100%
                span
                  display block
                  width 100%
                  text-align center
                  font-size 13px
                  color #666
          .swiper-pagination
            >span.swiper-pagination-bullet-active
              background #02a774
      .msite_shop_list
        top-border-1px(#e4e4e4)
        margin-top 10px
        background #fff
        .shop_header
          padding 10px 10px 0
          .shop_icon
            margin-left 5px
            color #999
          .shop_header_title
            color #000
            font-size 19px
            line-height 20px
            font-weight bold
    .dh
     position relative
     background #fff
     color #4B4B4B
     display flex
     justify-content space-between
     align-items center
     flex-wrap wrap
     padding 15px
    
     .a 
         display flex
         flex 1
         text-align center
         padding 15px 25px
         align-items center
         flex-direction column
         font-size 13px
         .image
             width 40px
             height 40px
             background-size 100% 100% 
             margin-bottom 5px
             background-repeat no-repeat
             
         .image1{
             bg-image2('../../pages/index/images/房源管理')
         }	
         .image2{
             bg-image2('../../pages/index/images/手机开门')
         }
         .image3{
             bg-image2('../../pages/index/images/账单管理')
         }
         .image4{
             bg-image2('../../pages/index/images/房屋报修')
         }
         .image5{
             bg-image2('../../pages/index/images/待办事项')
         }
         .image6{
             bg-image2('../../pages/index/images/楼栋管理')
         }        
  
    .shop_list
       display flex
       flex-direction row
       overflow scroll
      .shop_li
        bottom-border-1px(#f1f1f1)
        display flex
        flex-direction row
        >a
          clearFix()
          display block
          box-sizing border-box
          padding 15px 8px
          width 100%
          img
              width 160px 
              height 120px  
              margin-bottom 13px      
              border-radius 6px
         .detail
            width 160px
         .hostitle
              width 145px
              height 21px
              font-size 15px
              font-family PingFangSC-Medium, PingFang SC
              font-weight 500
              color #000000
              line-height 21px
         .hosproportion 
            width 81px
            height 17px
            font-size 12px
            font-family PingFang-SC-Regular, PingFang-SC
            font-weight 400
            color #4B4B4B
            line-height 17px
         .hosrentPrice
            font-size 18px
            font-familyPingFangSC-Medium, PingFang SC
            font-weight 500
            color #FC2631
            line-height 25px
        .hosrentPrice
            span 
             font-size 13px
        .hosbetp
             font-size 11px
             color #FF8D2C
             background-color #FFF5DF
             border-radius 2px
             width 45px
</style>